package com.mycjj.android.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CacheJsonDao{

	private static final String TABLENAME = "JSONCACHE_TB";
	private DBOpenHelper dbhelper;
	private SQLiteDatabase db; 

	private static final String DATABASE_NAME="project.db";
	
	public CacheJsonDao(Context context){
		dbhelper = new DBOpenHelper(context);
	}

	public void insertCacheData(String url,String content){
		db = dbhelper.getWritableDatabase();
		String sql = "INSERT OR REPLACE INTO " + TABLENAME + " (CACHE_ID,JSON_CONTENT) values (?,?)";
		String args[] = {url,content};
		db.execSQL(sql, args);
		db.close();
	}
	
	public String getCacheData(String url){
		db = dbhelper.getReadableDatabase();
		String content = null;
		Cursor c = db.rawQuery("select * from "+TABLENAME+" where CACHE_ID = " +"'"+url+"'", null);
		if (c != null && c.getCount() > 0) {
			while (c.moveToNext()) {
				content = c.getString(c.getColumnIndex("JSON_CONTENT"));
			}
//			c.close();
		}
		c.close();
		db.close();
		return content;
	}
	
	public void clearCacheData(){
		db = dbhelper.getWritableDatabase();
		String sql = "delete from  " + TABLENAME;
		db.execSQL(sql);
		db.close();
	}
}
